/*window.onload = function() {
        document.getElementById('text').innerHTML = 'Changed'
}*/

/*WHAT SHOULD BE IN GAME
food
water
limited actions
*/

//important variables
var actions = 3
var colEnd = 4
var gameOver = false

// functions for gathering actions
var food = 10
var water = 10
var wood = 15

function moreFood() {
    if(actions > 0){
        food+=10
        actions--
        console.log(food + ' | ' + actions)
        document.getElementById('actions').innerHTML = "Actions available: " + actions
    }
    document.getElementById('food').innerHTML = food
}

function moreWater() {
    if (actions > 0){
        water+=10
        actions--
        document.getElementById('actions').innerHTML = "Actions available: " + actions
    }
    document.getElementById('water').innerHTML = water
}

function moreWood() {
    if (actions > 0) {
        wood += 3
        actions--
        document.getElementById('actions').innerHTML = 'Actions available: ' + actions
    }
    document.getElementById('wood').innerHTML = wood
}

// building functions
var population = 8
var maxPop = 8
var houses = 2
homeCost = 50

function buildHouse() {
    if (wood >= homeCost && actions > 0) {
        houses++
        actions--
        wood -= homeCost
        homeCost += Math.floor(homeCost / 10)
        document.getElementById('homes').innerHTML = houses
        document.getElementById('wood').innerHTML = wood
        document.getElementById('homeMat').innerHTML = "Required: " + homeCost + " Wood"
        document.getElementById('actions').innerHTML = 'Actions available: ' + actions
        maxPop += 8
        document.getElementById('pop').innerHTML = 'Population: ' + population + '/' + maxPop
    } else if (wood < homeCost && actions > 0) {
        document.getElementById('warnings').innerHTML = 'Not enough wood!'
    }
}

function buildFarm() {

}

// end previous turn, start next turn
//include('./randEvents.js')
waterSurplus = false

function endTurn(){
    if (actions > 0) {
        document.getElementById('warnings').innerHTML = 'You still have remaining actions!'
        return
    }
    if (!gameOver) {
        food > 0 ? food-=population : food = 0
        water > 0 ? water-=population : water = 0
        waterSurplus ? water += population : water = water
        document.getElementById('warnings').innerHTML = ''
        if (water < 0 || food < 0) {
            colEnd--
            document.getElementById('warnings').innerHTML = 'Your colony has ' + colEnd + ' years remaining!'
            if (water < 0) {
                water = 0
                document.getElementById('water').innerHTML = water
            }
            if (food < 0) {
                food = 0
                document.getElementById('food').innerHTML = food
            }
        } else if (water > 0 && food > 0){
            colEnd = 3
        }

        if (maxPop - population != 0) {
            var popAddChance = Math.floor(Math.random() * 100)
            if (popAddChance >= 90) {
                population++
                document.getElementById('pop').innerHTML = 'Population: ' + population + '/' + maxPop
                document.getElementById('warnings').innerHTML = 'Someone has moved into town...'
            }
        }

        randEventPick = Math.floor(Math.random() * 100)
        console.log(randEventPick)

        if(randEventPick >= 90) {
            heavyRains()
        }
    }

    if (colEnd > 0) { actions = 3 }
    document.getElementById('food').innerHTML = food
    document.getElementById('water').innerHTML = water
    document.getElementById('actions').innerHTML = "Actions available: " + actions
    if (colEnd == 0) {
        document.getElementById('warnings').innerHTML = 'Game over!'
        gameOver = true
    }
}

function heavyRains() {
    document.getElementById('warnings').innerHTML = 'The colony faces heavy rains.'
    waterSurplus = true
} 